package com.alex.controller.Journal_Select;

import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchPhraseQuery;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.alex.common.config.IndexConfig;
import com.alex.entity.ArticleEntity;
import com.alex.service.AllQueryBasicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;


@RequestMapping("/journal_select")
@RestController
@Slf4j
public class JourSelectController extends AllQueryBasicService {

    @PostMapping(value = "journal_select")
    public String journal_select(String title) {
        log.info("Param: " + title);
        System.out.println("传进来的参数是: " + title);
        BoolQuery.Builder builder = new BoolQuery.Builder();
        builder.should(MatchPhraseQuery.of(i -> i.field("title").query(title).slop(0))._toQuery());
        builder.should(MatchPhraseQuery.of(i -> i.field("cnti").query(title).slop(0))._toQuery());
        List<Hit<ArticleEntity>> hits = null;
        try {
            hits = allQuery(IndexConfig.CORE_INDEX, builder.build()._toQuery(), null, ArticleEntity.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (null == hits) {
            log.info("Result: 0");
            System.out.println("Result: 0");

            return null;
        }
        List<ArticleEntity> coventList = coventList(hits);
        if (coventList.size() == 1) {
            log.info("Result: " + coventList.get(0).getJid());
            System.out.println("准备返回的数据是: " + coventList.get(0).getJid());
            return "" + coventList.get(0).getJid();
        }
        return null;
    }


}
